package com.fzh.controller.front;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fzh.pojo.dto.ProductLinkImageDTO;
import com.fzh.pojo.entity.Product;
import com.fzh.pojo.entity.ProductType;
import com.fzh.pojo.entity.User;
import com.fzh.service.ProductService;
import com.fzh.service.ProductTypeService;
import com.fzh.service.UserService;

@Controller
@RequestMapping("/front/")
public class LoginController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ProductTypeService productTypeService;
	
	/**
	 * 前台登录验证
	 * @param name
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public User login(User userInfo,HttpSession session,HttpServletRequest request) {	
		User user = userService.frontLogin(userInfo.getName(),userInfo.getLoginPassword());
		if(user != null) {
			session.setAttribute("user", user);	
			session.setAttribute("userId", user.getId());
		} else {
        	request.setAttribute("error", "用户名或密码错误!");
        }
		return user;
	}
	
	//退出登录
	@RequestMapping("/outLogin")
	@ResponseBody
	public String outLogin(HttpSession session){
		session.invalidate();
		return "success";
	}
	
	/**
	 * 登录成功后跳转到首页
	 */
	@RequestMapping("/toIndex")
	public String  toIndex(int userId,Model model,HttpSession session){		
		//查询轮播商品(查询没有下架,显示滚动图,图片权重降序limit4)
		List<Product> bannerPro = productService.selectBannerPro();
		//查询推荐商品(查询没有下架,不显示滚动图,产品权重降序limit3)
		List<Product> productTJ = productService.selectProductTJ();
		//查询全部商品(查询没有下架,权重降序)
		List<Product> productAll = productService.queryAllProduct();
		//查询全部分类(根据权重)
		List<ProductType> productTypes = productTypeService.selectAllProductTypeByPid();
		//一级分类
		List<ProductType> productTypes1 = productTypeService.selectAllProductTypeByP_id(0);
		//查询所有分类
		List<ProductType> productTypes2 = productTypeService.queryAllProduct();
		model.addAttribute("bannerPro", bannerPro);
		model.addAttribute("productTJ", productTJ);
		model.addAttribute("productAll", productAll);
		session.setAttribute("productTypes1", productTypes1);
		session.setAttribute("productTypes2", productTypes2);
		session.setAttribute("productTypes", productTypes);
		model.addAttribute("userId", userId);
		return "frontPage/index";
	}
	
	/**
	 *   注册一个用户
	 * @param user
	 * @return
	 */
	@RequestMapping("/add")
	public String add(User user) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = dateFormat.format(new Date());
		user.setBirthday(format);
		Boolean flag = userService.addUser(user);		
		if(flag){
			return "frontPage/login";
		}
		return "frontPage/reg";		
	}
}
